<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Article;
use App\Models\Article as AppArticle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AppArticle(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('cover_pic')->image();
            $grid->column('title');
            $grid->column('recommend')->switch();
            $grid->column('status')->switch();
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at');

            // $grid->column('label');
            // $grid->column('short_title');
            // $grid->column('keywords');
            // $grid->column('description');

            // $grid->column('content');
            // $grid->column('publish_time');
            // $grid->column('click');

            // $grid->column('sort');
            // $grid->column('cover_big');
            // $grid->column('author');
            // $grid->column('source');
            // $grid->column('annex');

            // $grid->column('author_label');
            // $grid->column('is_show_mobile');
            // $grid->column('pic_all');
            // $grid->column('show_type');
            // $grid->column('pic_ppt');
            // $grid->column('out_url');
            // $grid->column('out_status');
            // $grid->column('video_cover');
            // $grid->column('is_original');
            // $grid->column('praise_base');
            // $grid->column('praise_ratio');
            // $grid->column('video');
            // $grid->column('is_login');
            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('title');
                $filter->equal('recommend')->select([0 => '不推荐', 1 => '推荐']);
                $filter->equal('status')->select([0 => '隐藏', 1 => '显示']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Article(), function (Show $show) {
            $show->field('id');
            $show->field('cover_pic')->image();
            $show->field('title');
            // $show->field('label');
            // $show->field('short_title');
            // $show->field('keywords');
            $show->field('description');
            $show->field('content');
            $show->field('publish_time');
            $show->field('click');
            $show->field('status')->using(['0' => '隐藏', '1' => '显示'])->label();
            // $show->field('sort');
            // $show->field('cover_big');
            $show->field('author');
            $show->field('source');
            // $show->field('annex');
            $show->field('recommend')->using(['0' => '不推荐', '1' => '推荐'])->label();
            // $show->field('author_label');
            // $show->field('is_show_mobile');
            // $show->field('pic_all');
            // $show->field('show_type');
            // $show->field('pic_ppt');
            $show->field('out_url');
            // $show->field('out_status');
            // $show->field('video_cover');
            // $show->field('is_original');
            // $show->field('praise_base');
            // $show->field('praise_ratio');
            // $show->field('video');
            // $show->field('is_login');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppArticle(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->hidden('recommend')
                ->customFormat(function ($v) {
                    return $v == 1 ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v;
                });

            $form->hidden('status')
                ->customFormat(function ($v) {
                    return $v == 1 ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v;
                });
            $form->display('id');
            $form->image('cover_pic')->autoUpload()->move('articles/cover_pic/' . date('Y/m') . '/' . uniqid())->saveFullUrl();;
            $form->text('title');
            // $form->text('label');
            // $form->text('short_title');
            $form->text('keywords');
            $form->textarea('description')->rows(5);

            $form->editor('content');
            $form->datetime('publish_time');
            $form->text('click');
            $form->radio('status')->options([0 => '隐藏', 1 => '显示'])->default(1);
            $form->text('sort', '排序')->help('排序越大越靠前')->value(0);

            $form->text('author');
            $form->text('source');

            $form->radio('recommend')->options([0 => '不推荐', 1 => '推荐'])->default(1);
            // $form->text('author_label');
            // $form->text('is_show_mobile');
            // $form->text('pic_all');
            // $form->text('show_type');
            // $form->text('pic_ppt');
            $form->url('out_url');
            // $form->text('out_status');
            // $form->text('video_cover');
            // $form->text('is_original');
            // $form->text('praise_base');
            // $form->text('praise_ratio');
            // $form->text('video');
            // $form->text('is_login');

            // $form->saving(function (Form $form) {
            //     dd($form->input());
            // });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
